package Bai09;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import java.awt.FlowLayout;
import javax.swing.JButton;

@SuppressWarnings("serial")
public class Canvas extends JFrame 
	implements MouseListener,MouseMotionListener{
	private DatagramSocket socket;
	private DatagramPacket dp;
	private InetAddress ia;
	private int port = 5000;
	private JPanel panel=new JPanel();
	private JPanel canvas = new JPanel();
	private ButtonGroup bg=new ButtonGroup();
	private JRadioButton rbLine=new JRadioButton("Line",true);
	private JRadioButton rbTri=new JRadioButton("Triangle",false);
	private JRadioButton rbRect=new JRadioButton("Rectangle",false);
	private JRadioButton rbOval=new JRadioButton("Oval",false);
	private JRadioButton rbHexa=new JRadioButton("Hexagon",false);
	private Picture picture=new Picture();
	private Capture capture;
	private Color color=Color.RED;
	private Graphics g;
	private int kt=1;
	private float size;
	private Image img;
	private final JComboBox<String> cmb = new JComboBox<String>();
	private final JComboBox<String> comboBox = new JComboBox<String>();
	private final JButton btnUndo = new JButton("Undo");
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Canvas(final String Title) {
		super(Title);
		setSize(528,397);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		getContentPane().setLayout(null);
		panel.setBounds(0, 0, 518, 33);
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		getContentPane().add(panel);
		bg.add(rbOval);
		panel.add(rbOval);
		bg.add(rbTri);
		panel.add(rbTri);
		bg.add(rbLine);
		panel.add(rbLine);
		bg.add(rbRect);
		panel.add(rbRect);
		bg.add(rbHexa);
		panel.add(rbHexa);
		
		JButton btnSend = new JButton("Send");
		panel.add(btnSend);
		panel.add(btnUndo);
		btnUndo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				picture.remove();
				g=img.getGraphics();
				g.setColor(getBackground());
				g.fillRect(0,0,getWidth(),getHeight());
				picture.draw(g);
				g=canvas.getGraphics();
				g.drawImage(img, 0, 0, canvas);
			}
		});
		btnSend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				writeObjectToSocket(picture);
			}
		});
		rbHexa.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				kt=5;
			}
		});
		rbRect.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				kt=3;
			}
		});
		rbLine.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				kt=1;
			}
		});
		rbTri.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				kt=2;
			}
		});
		rbOval.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				kt=4;
			}
		});
		canvas.setBounds(0, 32, 348, 335);
		getContentPane().add(canvas);
		canvas.setBackground(this.getBackground());
		canvas.setLayout(null);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBounds(348, 32, 170, 327);
		getContentPane().add(panel_1);
		panel_1.setLayout(null);
		cmb.setBounds(70, 11, 73, 20);
		panel_1.add(cmb);
		cmb.setModel(new DefaultComboBoxModel(new String[] {"Red", "Green", "Blue", "Yellow", "Orange", "Pink", "Cyan", "Black"}));
		comboBox.setBounds(10, 11, 50, 20);
		panel_1.add(comboBox);
		comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String str=(String)comboBox.getSelectedItem();
				size=Float.parseFloat(str);
			}
		});
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3", "4", "8", "10", "14", "20", "24", "32"}));
		comboBox.setSelectedIndex(1);
		cmb.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String str=(String)cmb.getSelectedItem();
				switch(str){
				case "Red":color=Color.RED;break;
				case "Green":color=Color.GREEN;break;
				case "Blue":color=Color.BLUE;break;
				case "Yellow":color=Color.YELLOW;break;
				case "Orange":color=Color.ORANGE;break;
				case "Pink":color=Color.PINK;break;
				case "Cyan":color=Color.CYAN;break;
				case "Black":color=Color.BLACK;break;
				
				}
			}
		});
		canvas.addMouseListener(this);
		canvas.addMouseMotionListener(this);
		socketInit();	
	}
	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
		capture.setEndP(e.getPoint());
		
		g=img.getGraphics();
		g.setColor(getBackground());
		g.fillRect(0,0,getWidth(),getHeight());
		picture.draw(g);
		g=canvas.getGraphics();
		g.drawImage(img, 0, 0, canvas);
		//writeObjectToSocket(picture);
		
	}
	@Override
	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		switch(kt){
		case 1:capture=new Line();break;
		case 2:capture=new Triangle();break;
		case 3:capture=new Rectangle();break;
		case 4:capture=new Oval();break;
		case 5:capture=new Hexagon();break;
		}
		capture.setSize(size);
		capture.setColor(color);
		capture.setStartP(e.getPoint());
		capture.setEndP(e.getPoint());
		picture.add(capture);
		img=createImage(getWidth(), getHeight());
		g=img.getGraphics();
		picture.draw(g);
		g=canvas.getGraphics();
		g.drawImage(img, 0 ,0, canvas);
		//writeObjectToSocket(picture);
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		g.drawImage(img, 0, 0, canvas);
		//writeObjectToSocket(picture);
	}
	
	public void socketInit(){
		try {
			socket = new DatagramSocket();
			ia = InetAddress.getLocalHost();
		} catch (SocketException e) {
			e.printStackTrace();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}		
	}
	private void writeObjectToSocket(Picture pic) {
		ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
		try {
			ObjectOutputStream os = new ObjectOutputStream(byteStream);
			os.writeObject(pic);
			dp = new DatagramPacket(byteStream.toByteArray(), byteStream.size(), ia, port);
			socket.send(dp);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}		
	}
	
	
	public static void main(final String[] args) {
		new Canvas("Drag mouse to paint");
	}
}
